blob: 9bd403d8c306c44a3d6cf0feeab88c057e5aa3d0 [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)
214 : m_pAnnotHandlerMgr(NULL),
215 m_pActionHandler(NULL),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 m_pInfo(pFFinfo),
217 m_pSDKDoc(NULL),
218 m_pXFADoc(pDoc),
219 m_pIFormFiller(NULL) {
220 m_pSysHandler = new CFX_SystemHandler(this);
221}
222
223CPDFDoc_Environment::~CPDFDoc_Environment() {
224 delete m_pIFormFiller;
225 m_pIFormFiller = NULL;
226
227 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700228 if (pProvider->m_pEnvList.GetSize() == 0)
229 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230
231 delete m_pSysHandler;
232 m_pSysHandler = NULL;
233
234 delete m_pAnnotHandlerMgr;
235 m_pAnnotHandlerMgr = NULL;
236
237 delete m_pActionHandler;
238 m_pActionHandler = NULL;
239}
240
241int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
242 const FX_WCHAR* Title,
243 FX_UINT Type,
244 FX_UINT Icon) {
245 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
246 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
247 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
248 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
249 FPDF_WIDESTRING pTitle =
250 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
251 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
252 pTitle, Type, Icon);
253 bsMsg.ReleaseBuffer();
254 bsTitle.ReleaseBuffer();
255 return ret;
256 }
257 return -1;
258}
259
260int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
261 const FX_WCHAR* Title,
262 const FX_WCHAR* Default,
263 const FX_WCHAR* cLabel,
264 FPDF_BOOL bPassword,
265 void* response,
266 int length) {
267 if (m_pInfo && m_pInfo->m_pJsPlatform &&
268 m_pInfo->m_pJsPlatform->app_response) {
269 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
270 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
271 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
272 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
273 FPDF_WIDESTRING pQuestion =
274 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
275 FPDF_WIDESTRING pTitle =
276 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
277 FPDF_WIDESTRING pDefault =
278 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
279 FPDF_WIDESTRING pLabel =
280 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
281 int ret = m_pInfo->m_pJsPlatform->app_response(
282 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
283 response, length);
284 bsQuestion.ReleaseBuffer();
285 bsTitle.ReleaseBuffer();
286 bsDefault.ReleaseBuffer();
287 bsLabel.ReleaseBuffer();
288 return ret;
289 }
290 return -1;
291}
292
293CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
294 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
295 !m_pInfo->m_pJsPlatform->Field_browse) {
296 return L"";
297 }
298
299 const int nRequiredLen =
300 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
301 if (nRequiredLen <= 0)
302 return L"";
303
304 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
305 memset(pBuff.get(), 0, nRequiredLen);
306 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
307 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
308 if (nActualLen <= 0 || nActualLen > nRequiredLen)
309 return L"";
310
311 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
312 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
313 return wsRet;
314}
315
316CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
317 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
318 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
319 return L"";
320 }
321
322 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
323 m_pInfo->m_pJsPlatform, nullptr, 0);
324 if (nRequiredLen <= 0)
325 return L"";
326
327 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
328 memset(pBuff.get(), 0, nRequiredLen);
329 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
330 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
331 if (nActualLen <= 0 || nActualLen > nRequiredLen)
332 return L"";
333
334 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
335 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
336 return wsRet;
337}
338
339void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
340 int length,
341 const FX_WCHAR* URL) {
342 if (m_pInfo && m_pInfo->m_pJsPlatform &&
343 m_pInfo->m_pJsPlatform->Doc_submitForm) {
344 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
345 FPDF_WIDESTRING pDestination =
346 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
347 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
348 length, pDestination);
349 bsDestination.ReleaseBuffer();
350 }
351}
352
353void CPDFDoc_Environment::JS_docmailForm(void* mailData,
354 int length,
355 FPDF_BOOL bUI,
356 const FX_WCHAR* To,
357 const FX_WCHAR* Subject,
358 const FX_WCHAR* CC,
359 const FX_WCHAR* BCC,
360 const FX_WCHAR* Msg) {
361 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
362 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
363 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
364 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
365 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
366 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
367 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
368 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
369 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
370 FPDF_WIDESTRING pSubject =
371 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
372 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
373 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
374 bUI, pTo, pSubject, pCC, pBcc, pMsg);
375 bsTo.ReleaseBuffer();
376 bsCC.ReleaseBuffer();
377 bsBcc.ReleaseBuffer();
378 bsSubject.ReleaseBuffer();
379 bsMsg.ReleaseBuffer();
380 }
381}
382
Tom Sepezba038bc2015-10-08 12:03:00 -0700383IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700385 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700387 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700388 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389}
390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
392 if (!m_pAnnotHandlerMgr)
393 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
394 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700395}
396
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
398 if (!m_pActionHandler)
Tom Sepez3b5f1242015-09-01 14:06:55 -0700399 m_pActionHandler = new CPDFSDK_ActionHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700401}
402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
404 if (!m_pIFormFiller)
405 m_pIFormFiller = new CFFL_IFormFiller(this);
406 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407}
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
410 CPDFDoc_Environment* pEnv)
411 : m_pDoc(pDoc),
412 m_pInterForm(nullptr),
413 m_pFocusAnnot(nullptr),
414 m_pEnv(pEnv),
415 m_pOccontent(nullptr),
Oliver Chang972b78d2015-10-30 12:59:29 -0700416 m_bChangeMask(FALSE),
417 m_bBeingDestroyed(FALSE) {
418}
Bo Xufdc00a72014-10-28 23:03:33 -0700419
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700421 m_bBeingDestroyed = TRUE;
422
423 for (auto& it : m_pageMap)
424 it.second->KillFocusAnnotIfNeeded();
425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 for (auto& it : m_pageMap)
427 delete it.second;
428 m_pageMap.clear();
429
430 delete m_pInterForm;
431 m_pInterForm = nullptr;
432
433 delete m_pOccontent;
434 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700435}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
438 FX_BOOL ReNew) {
439 auto it = m_pageMap.find(pPDFXFAPage);
440 if (it != m_pageMap.end())
441 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 if (!ReNew)
444 return nullptr;
445
446 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
447 m_pageMap[pPDFXFAPage] = pPageView;
448 // Delay to load all the annotations, to avoid endless loop.
449 pPageView->LoadFXAnnots();
450 return pPageView;
451}
452
453CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
454 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
455 return pPage ? GetPageView(pPage, TRUE) : nullptr;
456}
457
458CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
459 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
460 if (!pTempPage)
461 return nullptr;
462
463 auto it = m_pageMap.find(pTempPage);
464 return it->second;
465}
466
467void CPDFSDK_Document::ProcJavascriptFun() {
468 CPDFXFA_Document* pPDFDoc = GetDocument();
469 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
470 int iCount = docJS.CountJSActions();
471 if (iCount < 1)
472 return;
473 for (int i = 0; i < iCount; i++) {
474 CFX_ByteString csJSName;
475 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
476 if (m_pEnv->GetActionHander())
477 m_pEnv->GetActionHander()->DoAction_JavaScript(
478 jsAction, CFX_WideString::FromLocal(csJSName), this);
479 }
480}
481
482FX_BOOL CPDFSDK_Document::ProcOpenAction() {
483 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700484 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485
486 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
487 if (!pRoot)
488 return FALSE;
489
490 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
491 if (!pOpenAction)
492 pOpenAction = pRoot->GetArray("OpenAction");
493
494 if (!pOpenAction)
495 return FALSE;
496
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400497 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 return TRUE;
499
Dan Sinclairf1251c12015-10-20 16:24:45 -0400500 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501 CPDF_Action action(pDict);
502 if (m_pEnv->GetActionHander())
503 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
504 return TRUE;
505 }
506 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700507}
508
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
510 if (!m_pOccontent)
511 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
512 return m_pOccontent;
513}
Bo Xufdc00a72014-10-28 23:03:33 -0700514
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
516 auto it = m_pageMap.find(pPDFXFAPage);
517 if (it == m_pageMap.end())
518 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700519
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 CPDFSDK_PageView* pPageView = it->second;
521 if (pPageView->IsLocked())
522 return;
523
Oliver Chang972b78d2015-10-30 12:59:29 -0700524 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 delete pPageView;
526 m_pageMap.erase(it);
527}
528
529CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
530 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
531 if (!pTempPage)
532 return NULL;
533 return pTempPage;
534}
535
536CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
537 if (!m_pInterForm)
538 m_pInterForm = new CPDFSDK_InterForm(this);
539 return m_pInterForm;
540}
541
542void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
543 CPDFSDK_Annot* pAnnot) {
544 for (const auto& it : m_pageMap) {
545 CPDFSDK_PageView* pPageView = it.second;
546 if (pPageView != pSender) {
547 pPageView->UpdateView(pAnnot);
548 }
549 }
550}
551
552CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
553 return m_pFocusAnnot;
554}
555
556FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700557 if (m_bBeingDestroyed)
558 return FALSE;
559
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 if (m_pFocusAnnot == pAnnot)
561 return TRUE;
562
563 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
564
565 if (m_pFocusAnnot) {
566 if (!KillFocusAnnot(nFlag))
567 return FALSE;
568 }
569 CPDFSDK_PageView* pPageView = NULL;
570 if (pAnnot)
571 pPageView = pAnnot->GetPageView();
572 if (pAnnot && pPageView->IsValid()) {
573 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700574 if (!m_pFocusAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700576 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700577
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
579 return FALSE;
580 if (!m_pFocusAnnot) {
581 m_pFocusAnnot = pAnnot;
582 return TRUE;
583 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700584 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 }
586 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700587}
588
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
590 if (m_pFocusAnnot) {
591 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700592 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
593 m_pFocusAnnot = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700594
Lei Zhanga0601462015-10-30 14:54:37 -0700595 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
596 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700597
Lei Zhanga0601462015-10-30 14:54:37 -0700598 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
599 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
600 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
601 int nFieldType = pWidget->GetFieldType();
602 if (FIELDTYPE_TEXTFIELD == nFieldType ||
603 FIELDTYPE_COMBOBOX == nFieldType) {
604 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 }
Lei Zhanga0601462015-10-30 14:54:37 -0700607
608 if (!m_pFocusAnnot)
609 return TRUE;
610 } else {
611 m_pFocusAnnot = pFocusAnnot;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700612 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613 }
614 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700615}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700616
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
618 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
619 return FALSE;
620 }
621
622 CPDFXFA_Page* pTempPage = NULL;
623 for (int i = nCount - 1; i >= 0; i--) {
624 pTempPage = GetPage(nStart + i);
625 if (pTempPage != NULL) {
626 ReMovePageView(pTempPage);
627 }
628 }
629 return TRUE;
630}
631
632void CPDFSDK_Document::OnCloseDocument() {
633 KillFocusAnnot();
634}
635
636FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
637 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
638 return dwPermissions & nFlag;
639}
640
Tom Sepezba038bc2015-10-08 12:03:00 -0700641IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 ASSERT(m_pEnv != NULL);
643 return m_pEnv->GetJSRuntime();
644}
645
646CFX_WideString CPDFSDK_Document::GetPath() {
647 ASSERT(m_pEnv != NULL);
648 return m_pEnv->JS_docGetFilePath();
649}
650
651CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
652 CPDFXFA_Page* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700653 : m_page(page),
654 m_pSDKDoc(pSDKDoc),
655 m_CaptureWidget(nullptr),
656 m_bEnterWidget(FALSE),
657 m_bExitWidget(FALSE),
658 m_bOnWidget(FALSE),
659 m_bValid(FALSE),
660 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
662 if (pInterForm) {
663 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
664 if (page->GetPDFPage())
665 pPDFInterForm->FixPageFields(page->GetPDFPage());
666 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667}
668
669CPDFSDK_PageView::~CPDFSDK_PageView() {
670 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700671 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700672 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700674 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675
Lei Zhangbf60b292015-10-26 12:14:35 -0700676 m_pAnnotList.reset();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677}
678
679void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
680 CPDF_Matrix* pUser2Device,
681 CPDF_RenderOptions* pOptions,
682 FX_RECT* pClip) {
683 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700684 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
685 CPDFXFA_Page* pPage = GetPDFXFAPage();
686
687 if (pPage == NULL)
688 return;
689
Tom Sepeza8a39e22015-10-12 15:47:07 -0700690#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
692 CFX_Graphics gs;
693 gs.Create(pDevice);
694 if (pClip) {
695 CFX_RectF rectClip;
696 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
697 static_cast<FX_FLOAT>(pClip->top),
698 static_cast<FX_FLOAT>(pClip->Width()),
699 static_cast<FX_FLOAT>(pClip->Height()));
700 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700701 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
703 if (!pRenderContext)
704 return;
705 CXFA_RenderOptions renderOptions;
706 renderOptions.m_bHighlight = TRUE;
707 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
708 renderOptions);
709 pRenderContext->DoRender();
710 pRenderContext->StopRender();
711 pRenderContext->Release();
712 return;
713 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700714#endif // PDF_ENABLE_XFA
715
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700716 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700717 CPDFSDK_AnnotIterator annotIterator(this, true);
718 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 }
722}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700723
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
725 FX_FLOAT pageY) {
Lei Zhang50218532015-10-30 14:03:43 -0700726 const int nCount = m_pAnnotList->Count();
727 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
729 CFX_FloatRect annotRect;
730 pAnnot->GetRect(annotRect);
731 if (annotRect.Contains(pageX, pageY))
732 return pAnnot;
733 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700734 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735}
736
737CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
738 FX_FLOAT pageY) {
Lei Zhang50218532015-10-30 14:03:43 -0700739 const int nCount = m_pAnnotList->Count();
Lei Zhangbf60b292015-10-26 12:14:35 -0700740 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
742 if (pAnnot->GetSubType() == "Widget") {
743 CFX_FloatRect annotRect;
744 pAnnot->GetRect(annotRect);
745 if (annotRect.Contains(pageX, pageY))
746 return pAnnot;
747 }
748 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700749 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750}
751
752CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
753 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
755 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700756 CPDFSDK_AnnotIterator annotIterator(this, false);
757 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
759 if (rc.Contains(pageX, pageY))
760 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700761 }
762
Lei Zhangbf60b292015-10-26 12:14:35 -0700763 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700764}
765
766CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
767 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
769 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700770 CPDFSDK_AnnotIterator annotIterator(this, false);
771 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772 if (pSDKAnnot->GetType() == "Widget" ||
773 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
774 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
775 CPDF_Point point(pageX, pageY);
776 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
777 return pSDKAnnot;
778 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700779 }
780
Lei Zhangbf60b292015-10-26 12:14:35 -0700781 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782}
783
Oliver Chang972b78d2015-10-30 12:59:29 -0700784void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
785 // if there is a focused annot on the page, we should kill the focus first.
786 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
787 auto it =
788 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
789 if (it != m_fxAnnotArray.end())
790 KillFocusAnnot();
791 }
792}
793
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
795 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
796 if (pAnnotDic)
797 return pAnnotDic->KeyExist("AS");
798 return FALSE;
799}
800
801CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
802 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
803 ASSERT(pEnv);
804 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700805 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700806 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700807 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700808
Lei Zhangbf60b292015-10-26 12:14:35 -0700809 m_fxAnnotArray.push_back(pSDKAnnot);
810 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 return pSDKAnnot;
812}
813
814CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
815 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700816 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817
818 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
819 if (pSDKAnnot)
820 return pSDKAnnot;
821
822 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700824 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700826 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827
Lei Zhangbf60b292015-10-26 12:14:35 -0700828 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 return pSDKAnnot;
830}
831
832CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
833 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
834}
835
836CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
837 CPDF_Dictionary* pDict) {
838 return NULL;
839}
840
841FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
842 if (!pAnnot)
843 return FALSE;
844 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
845 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
846 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
847 return FALSE;
848
Lei Zhangbf60b292015-10-26 12:14:35 -0700849 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
850 if (it != m_fxAnnotArray.end())
851 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700853 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854
855 return TRUE;
856}
857
858CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
859 if (m_page) {
860 return m_page->GetDocument()->GetPDFDoc();
861 }
862 return NULL;
863}
864
865CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
866 if (m_page) {
867 return m_page->GetPDFPage();
868 }
869
870 return NULL;
871}
872
Lei Zhang50218532015-10-30 14:03:43 -0700873size_t CPDFSDK_PageView::CountAnnots() const {
874 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875}
876
Lei Zhangbf60b292015-10-26 12:14:35 -0700877CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
878 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879}
880
881CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700882 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
883 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 return pAnnot;
885 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700886 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887}
888CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700889 if (!hWidget)
890 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891
Lei Zhangbf60b292015-10-26 12:14:35 -0700892 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 if (pAnnot->GetXFAWidget() == hWidget)
894 return pAnnot;
895 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700896 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897}
898
899FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
900 FX_UINT nFlag) {
901 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
902 ASSERT(pEnv);
903 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
904 if (!pFXAnnot) {
905 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700906 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907 }
Lei Zhanga0601462015-10-30 14:54:37 -0700908
909 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
910 FX_BOOL bRet =
911 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
912 if (bRet)
913 SetFocusAnnot(pFXAnnot);
914 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700915}
916
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
918 FX_UINT nFlag) {
919 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
920 ASSERT(pEnv);
921 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
922 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700923
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
925
926 if (pFXAnnot == NULL)
927 return FALSE;
928
929 FX_BOOL bRet =
930 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
931 if (bRet) {
932 SetFocusAnnot(pFXAnnot);
933 }
934 return TRUE;
935}
936
937FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
938 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
939 ASSERT(pEnv);
940 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941
942 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
943
944 if (pFXAnnot == NULL)
945 return FALSE;
946
947 FX_BOOL bRet =
948 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
949 if (bRet) {
950 SetFocusAnnot(pFXAnnot);
951 }
952 return TRUE;
953}
954
955FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
956 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
957 ASSERT(pEnv);
958 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
960 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
961 FX_BOOL bRet = FALSE;
962 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
963 // Last focus Annot gets a chance to handle the event.
964 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
965 }
Lei Zhanga0601462015-10-30 14:54:37 -0700966 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 return bRet;
969}
970
971FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
972 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
973 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
975 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
976 m_bExitWidget = TRUE;
977 m_bEnterWidget = FALSE;
978 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
979 }
980 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
981 m_bOnWidget = TRUE;
982 if (!m_bEnterWidget) {
983 m_bEnterWidget = TRUE;
984 m_bExitWidget = FALSE;
985 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
986 }
987 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
988 return TRUE;
989 }
990 if (m_bOnWidget) {
991 m_bOnWidget = FALSE;
992 m_bExitWidget = TRUE;
993 m_bEnterWidget = FALSE;
994 if (m_CaptureWidget) {
995 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
996 m_CaptureWidget = NULL;
997 }
998 }
999 return FALSE;
1000}
1001
1002FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1003 double deltaY,
1004 const CPDF_Point& point,
1005 int nFlag) {
1006 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001007 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1008 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1010 (int)deltaY, point);
1011 }
1012 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001013}
1014
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1016 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001017 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001018 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1020 }
Bo Xufdc00a72014-10-28 23:03:33 -07001021
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001023}
1024
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1026 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001027 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1030 }
1031 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001032}
1033
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001036}
1037
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041
1042 SetLock(TRUE);
1043 m_page->AddRef();
1044 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1045 IXFA_PageView* pageView = NULL;
1046 pageView = m_page->GetXFAPageView();
1047 ASSERT(pageView != NULL);
1048
1049 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1050 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1051 XFA_WIDGETFILTER_Viewable |
1052 XFA_WIDGETFILTER_AllType);
1053 if (!pWidgetHander) {
1054 m_page->Release();
1055 SetLock(FALSE);
1056 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001057 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058
Lei Zhangbf60b292015-10-26 12:14:35 -07001059 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001061 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063
Lei Zhangbf60b292015-10-26 12:14:35 -07001064 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 }
1067
1068 pWidgetHander->Release();
1069 } else {
1070 CPDF_Page* pPage = m_page->GetPDFPage();
1071 ASSERT(pPage != NULL);
1072
1073 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1074 // Disable the default AP construction.
1075 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001076 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1078
Lei Zhang50218532015-10-30 14:03:43 -07001079 const int nCount = m_pAnnotList->Count();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 for (int i = 0; i < nCount; i++) {
1081 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001082 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083
1084 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1085 if (!pAnnot)
1086 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001087 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088
1089 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1090 }
1091 }
1092 m_page->Release();
1093 SetLock(FALSE);
1094}
1095
1096void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1097 for (int i = 0; i < rects.GetSize(); i++) {
1098 CPDF_Rect rc = rects.GetAt(i);
1099 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1100 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1101 }
1102}
1103
1104void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1105 CPDF_Rect rcWindow = pAnnot->GetRect();
1106 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1107 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1108 rcWindow.bottom);
1109}
1110
1111int CPDFSDK_PageView::GetPageIndex() {
1112 if (m_page) {
1113 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1114 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1115 if (pDoc && pDic) {
1116 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1117 }
1118 }
1119 return -1;
1120}
1121
Lei Zhangbf60b292015-10-26 12:14:35 -07001122FX_BOOL CPDFSDK_PageView::IsValidAnnot(CPDF_Annot* p) const {
1123 if (!p)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001124 return FALSE;
Lei Zhangbf60b292015-10-26 12:14:35 -07001125
Lei Zhang50218532015-10-30 14:03:43 -07001126 const int nCount = m_pAnnotList->Count();
Lei Zhangbf60b292015-10-26 12:14:35 -07001127 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001128 if (m_pAnnotList->GetAt(i) == p)
1129 return TRUE;
1130 }
1131 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001132}
1133
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1135 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1136 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001137 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138
Lei Zhangbf60b292015-10-26 12:14:35 -07001139 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140 if (pAnnot == pFocusAnnot)
1141 return pAnnot;
1142 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001143 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001144}